package main

/**
 * @Author: DengLibin
 * @Date: Create in 2023-01-11 12:07:24
 * @Description: 反转链表 迭代
 */
func reverseList(head *ListNode) *ListNode {
	if head == nil {
		return nil
	}
	var preNode *ListNode = nil
	var nextNode *ListNode = nil
	currentNode := head

	for currentNode != nil {
		nextNode = currentNode.Next
		currentNode.Next = preNode
		preNode = currentNode
		currentNode = nextNode
	}
	return preNode
}

/**
 * @Author: DengLibin
 * @Date: Create in 2023-01-11 12:20:04
 * @Description: 反转链表 递归
 */
func reverseList2(head *ListNode) *ListNode {
	if head == nil || head.Next == nil {
		return head
	}

	h := reverseList2(head.Next)
	head.Next.Next = head
	head.Next = nil

	return h
}
